# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer:
pkgname=mingw-w64-gcc-base
pkgver=11.3.0
pkgrel=0
_islver=0.22
pkgdesc="Cross GCC for the MinGW-w64 cross-compiler (bootstrap)"
url="https://gcc.gnu.org/"
arch="x86_64 x86"
license="GPL-3.0-or-later"
depends="mingw-w64-binutils mingw-w64-headers mingw-w64-headers-bootstrap !mingw-w64-gcc"
makedepends="zlib-dev mpc1-dev gmp-dev mpfr-dev"
source="
	$pkgname-$pkgver.tar.xz::https://ftp.gnu.org/gnu/gcc/gcc-$pkgver/gcc-$pkgver.tar.xz
	https://libisl.sourceforge.io/isl-$_islver.tar.bz2
	"
builddir="$srcdir/gcc-$pkgver"

case "$CARCH" in
	x86_64) _target="x86_64-w64-mingw32" ;;
	x86) _target="i686-w64-mingw32" ;;
esac

# Directory for our out-of-tree builds
_gccdir="$srcdir"/gcc-build-$_target

prepare() {
	default_prepare

	# Do not install libiberty
	sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
	sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" gcc/configure

	# link isl for in-tree builds
	ln -sf ../isl-$_islver isl

	mkdir -p "$_gccdir"
}

build() {
	cd "$_gccdir"
	"$builddir"/configure \
		--prefix=/usr \
		--target=$_target \
		--enable-languages=c,lto \
		--enable-static \
		--with-system-zlib \
		--enable-lto --disable-dw2-exceptions \
		--disable-ns --enable-version-specific-runtime-libs \
		--disable-multilib --enable-checking=release \
		CFLAGS="$(echo "$CFLAGS" | sed -e 's/-Os/-O2/g')" \
		CPPFLAGS="$(echo "$CPPFLAGS" | sed -e 's/-Os/-O2/g')"
	make all-gcc
}

package() {
	cd "$_gccdir"
	make DESTDIR="$pkgdir" install-gcc

	strip "$pkgdir"/usr/bin/$_target-*
	strip "$pkgdir"/usr/libexec/gcc/$_target/$pkgver/lto*
	strip "$pkgdir"/usr/libexec/gcc/$_target/$pkgver/cc1
	strip "$pkgdir"/usr/libexec/gcc/$_target/$pkgver/collect2

	rm -rf "$pkgdir"/usr/share/man
	rm -rf "$pkgdir"/usr/share/info
	rm -rf "$pkgdir"/usr/share/locale
}

sha512sums="
f0be5ad705c73b84477128a69c047f57dd47002f375eb60e1e842e08cf2009a509e92152bca345823926d550b7395ae6d4de7db51d1ee371c2dc37313881fca7  mingw-w64-gcc-base-11.3.0.tar.xz
fc2c9796979610dd51143dcefe4f5c989c4354571cc5a1fcc6b932fd41f42a54f6b43adfd289af61be7bd06f3a523fa6a7d7ee56680e32d8036beb4c188fa668  isl-0.22.tar.bz2
"
